local lifen = fk.CreateSkill{
  name = "rfenghou_3d17j__lifen",
}

lifen:addEffect(fk.RoundStart, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(lifen.name)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local tos = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player, false),
      min_num = 1,
      max_num = 1,
      skill_name = lifen.name,
      prompt = "#rfenghou_3d17j__lifen-invoke",
    })
    if tos[1] then
      event:setCostData(self, { tos = tos })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = table.connect({ player }, event:getCostData(self).tos or {})
    room:sortByAction(tos)
    for _, to in ipairs(tos) do
      room:addTableMark(to, "@@rfenghou_3d17j__lifen", player.id)
      room:handleAddLoseSkills(to, "rfenghou_3d17j__jigong")
    end
  end
})
lifen:addEffect(fk.RoundEnd, {
  is_delay_effect = true,
  mute = true,
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    for _, to in ipairs(player.room:getAlivePlayers(false)) do
      if table.contains(to:getTableMark("@@rfenghou_3d17j__lifen"), player.id) then
        return true
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = {}
    local your_state
    for _, to in ipairs(room:getAlivePlayers()) do
      if table.contains(to:getTableMark("@@rfenghou_3d17j__lifen"), player.id) then
        if to == player then
          your_state = to:getMark("rfenghou_3d17j__jigong_choice-noclear") == 0
        else
          tos[to] = to:getMark("rfenghou_3d17j__jigong_choice-noclear") == 0
        end
        room:setPlayerMark(to, "rfenghou_3d17j__jigong_choice-noclear", 0)
        room:removeTableMark(to, "@@rfenghou_3d17j__lifen", player.id)
        if to:getMark("@@rfenghou_3d17j__lifen") == 0 then
          room:handleAddLoseSkills(to, "-rfenghou_3d17j__jigong")
        end
      end
    end
    for to, state in pairs(tos) do
      if your_state == state then
        local prompt = "#rfenghou_3d17j__lifen-view"
        if not player:isKongcheng() then
          prompt = prompt .. "give"
        end
        prompt = prompt .. "::" .. to.id

        local poxiParams = { ---@type AskToPoxiParams
          poxi_type = "AskForCardsChosen",
          data = {
            {"$Hand", to:getCardIds("h")},
            {"$Equip", to:getCardIds("e")},
          },
          extra_data = {
            to = to.id,
            min = 1,
            max = 1,
            skillName = lifen.name,
            prompt = prompt,
          },
          cancelable = not player:isKongcheng()
        }

        local ret = room:askToPoxi(player, poxiParams)
        if #ret ~= 0 then
          room:obtainCard(player, ret, false, fk.ReasonPrey, player, lifen.name)
        elseif not player:isKongcheng() then
          local cards = room:askToCards(player, {
            min_num = 1,
            max_num = 1,
            include_equip = true,
            skill_name = lifen.name,
            prompt = "#rfenghou_3d17j__lifen-give::" .. to.id,
            cancelable = false
          })
          room:obtainCard(to, cards, false, fk.ReasonGive, player, lifen.name)
        end
      else
        room:damage{
          from = player,
          to = to,
          damage = 1,
          skillName = lifen.name
        }
      end
    end
  end
})

Fk:loadTranslationTable{
  ["rfenghou_3d17j__lifen"] = "理棼",
  [":rfenghou_3d17j__lifen"] = "轮次开始时，你可以令自己与一名其他角色于此轮内获得<a href=':rfenghou_3d17j__jigong'>〖急攻〗</a>；轮次结束时，若你与其于本轮在是否发动〖急攻〗的选择上一致/不一致，你观看其手牌并获得或交给其一张牌/对其造成1点伤害 。",

  ["#rfenghou_3d17j__lifen-invoke"] = "理棼：你可以令自己与一名其他角色于此轮内获得〖急攻〗",
  ["#rfenghou_3d17j__lifen-view"] = "理棼：请获得 %dest 一张牌",
  ["#rfenghou_3d17j__lifen-viewgive"] = "理棼：请获得 %dest 一张牌，否则你交给其一张牌",
  ["#rfenghou_3d17j__lifen-give"] = "理棼：你须交给 %dest 一张牌",

  ["@@rfenghou_3d17j__lifen"] = "理棼 急攻",
}

return lifen
